package com.jinlejia.erp.ui.new_house.house;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityHouseTypeListBinding;
import com.jinlejia.erp.ui.common.BundleArrayListBean;
import com.jinlejia.erp.ui.common.BundleLinkedHashMap;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/***
 * 户型列表Activity
 */
public class HouseTypeListActivity extends AppCompatActivity {
    private ActivityHouseTypeListBinding binding;
    private ArrayList<Map<String, Object>> allDataList = new ArrayList<>();
    private LinkedHashMap<String, ArrayList<Map<String, Object>>> map;
    private String mTitleArray[] = new String[20];
    private List<Fragment> mFragments = new ArrayList<>();
    ;
    private InnerViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_house_type_list);
        binding.setEvent(this);
        EventBus.getDefault().registerSticky(this);
    }

    @Subscriber(tag = "TAG_HOUSETYPEONE_LIST")
    public void getHouseIntroduce(LinkedHashMap<String, ArrayList<Map<String, Object>>> houseTypeMap) {
        EventBus.getDefault().removeStickyEvent(houseTypeMap.getClass(), "TAG_HOUSETYPEONE_LIST");
        map = houseTypeMap;
        int allSize = 0;
        int i = 0;
        Iterator<Map.Entry<String, ArrayList<Map<String, Object>>>> iterator = houseTypeMap.entrySet().iterator();  //map.entrySet()得到的是set集合，可以使用迭代器遍历
        while (iterator.hasNext()) {
            Map.Entry<String, ArrayList<Map<String, Object>>> entry = iterator.next();
            allSize = allSize + entry.getValue().size();
            allDataList.addAll(entry.getValue());
        }
        mTitleArray[i] = "全部" + "(" + allSize + ")";
        mFragments.add(i, HouseTypeListFragment.newInstance(new BundleArrayListBean(allDataList),new BundleLinkedHashMap(map)));

        for (Map.Entry<String, ArrayList<Map<String, Object>>> entry : houseTypeMap.entrySet()) {
            ArrayList<Map<String, Object>> value = entry.getValue();
            mTitleArray[i + 1] = entry.getKey() + "(" + value.size() + ")";
            mFragments.add(i + 1, HouseTypeListFragment.newInstance(new BundleArrayListBean(value),new BundleLinkedHashMap(map)));
            i = i + 1;
        }


        viewPagerAdapter = new InnerViewPagerAdapter(this.getSupportFragmentManager(), mTitleArray, mFragments);
        binding.viewPager.setAdapter(viewPagerAdapter);
        binding.viewPager.setOffscreenPageLimit(mFragments.size());

        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        binding.idTableLayout.setTabsFromPagerAdapter(viewPagerAdapter);
        binding.idTableLayout.post(() -> {
            // 将TabLayout和ViewPager进行关联，让两者联动起来
            binding.idTableLayout.setupWithViewPager(binding.viewPager);
        });

    }

    public void onClick(View view) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private static class InnerViewPagerAdapter extends FragmentStatePagerAdapter {
        private String[] mTitles;
        private List<Fragment> mFragments;

        public InnerViewPagerAdapter(FragmentManager fm, String[] mTitles, List<Fragment> mFragments) {
            super(fm);
            this.mTitles = mTitles;
            this.mFragments = mFragments;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

    }
}